package cn.jaye.fabric.controller;

import cn.jaye.fabric.common.constant.Global;
import cn.jaye.fabric.application.AuthApplicationService;
import cn.jaye.fabric.application.command.LoginCommand;
import cn.jaye.fabric.common.constant.ExceptionEnum;
import cn.jaye.fabric.common.exception.ServiceException;
import cn.jaye.fabric.common.util.CookieUtil;
import cn.jaye.fabric.common.util.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Jaye
 * @create 2017-12-20 17:14
 */
@Controller
public class IndexController {

    @Autowired
    private AuthApplicationService authApplicationService;

    @PostMapping("/ping")
    @ResponseBody
    public String ping() {
        return "ok";
    }

    @RequestMapping("/")
    public String index() {
        return "redirect:/index";
    }

    @RequestMapping("/500")
    public String error500() {
        return "500";
    }


    @RequestMapping("/index")
    public String defaultIndex(HttpServletRequest request, Model model) {
        model.addAttribute("username","Jaye");
//        model.addAttribute("username",request.getSession().getAttribute("username"));
        return "index";
    }

    @RequestMapping("/accesspoint")
    public String accesspoint() {
        return "accesspoint";
    }

    @RequestMapping("/index_v1")
    public String index_v1() {
        return "index_v1";
    }

    @RequestMapping("/index_v2")
    public String index_v2() {
        return "index_v2";
    }

    @RequestMapping("/form/accesspoint/create")
    public String createFormAccesspoint() {
        return "form_validate";
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/dologin")
    @ResponseBody
    public String doLogin(HttpServletRequest request, @ModelAttribute LoginCommand loginCommand) {
//        if ("admin".equals(loginCommand.getUsername()) && "123".equals(loginCommand.getPassword())) {
//            CookieUtil.addCookie(response, Global.COOKIE_NAME, "11111111", Global.COOKIE_MAX_AGE);
//            return ResultUtil.success(null);
//        }
        if (StringUtils.isNotBlank(loginCommand.getUsername()) && StringUtils.isNotBlank(loginCommand.getPassword())) {
            if (authApplicationService.login(loginCommand)) {
                request.getSession().setAttribute("username", loginCommand.getUsername());
                request.getSession().setAttribute("password", loginCommand.getPassword());
                request.getSession().setAttribute("orgId", loginCommand.getOrgId());
                return ResultUtil.success(null);
            } else {
                return ResultUtil.error(ExceptionEnum.LOGIN_USERNAME_PASSWORD_ERROR);
            }
        } else {
            throw new ServiceException(ExceptionEnum.INVALID_PARAMETER);
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpServletResponse response, HttpServletRequest request) {
        Cookie cookie = CookieUtil.getCookieByName(request, Global.COOKIE_NAME);
        if (cookie != null && StringUtils.isNotBlank(cookie.getValue())) {
            authApplicationService.logout(cookie.getValue());
        }
        clearCookies(request, response);
        return "redirect:/login";
    }

    private void clearCookies(HttpServletRequest request, HttpServletResponse response) {
        Cookie cookies[] = request.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals(Global.COOKIE_NAME)) {
                    Cookie cookie = new Cookie(Global.COOKIE_NAME, "clear");//这边得用"",不能用null
                    cookie.setPath("/");//设置成跟写入cookies一样的
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
            }
        }
    }

    @RequestMapping("/secretkey")
    public String table_bootstrap() {
        return "accesspoint_secretkey";
    }

    @RequestMapping("/api")
    public String api() {
        return "api";
    }

    @RequestMapping("/apipackage")
    public String servicepackage() {
        return "apiPackage";
    }

    @RequestMapping("/tenantapipackage")
    public String apiservicepackage() {
        return "tenantApiPackage";
    }

    @RequestMapping("/commodity")
    public String commodity() {
        return "commodity";
    }

    @RequestMapping("/peer")
    public String peer() {
        return "page/peer";
    }

    @RequestMapping("/chaincode")
    public String chaincode() {
        return "page/chaincode";
    }
    @RequestMapping("/deploy")
    public String deploy() {
        return "page/deploy";
    }
    @RequestMapping("/createDeploy")
    public String newDeploy() {
        return "page/createDeploy";
    }
    @RequestMapping("/browser")
    public String browser() {
        return "page/browser";
    }
}
